﻿@{
    ViewBag.Title = "Detail";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/Content/scripts/plugins/jqgrid/jqgrid.css" rel="stylesheet" />
<script src="~/Content/scripts/plugins/jqgrid/jqgrid.min.js"></script>
<script>
    var keyValue = request('keyValue');
    var ContractId = request('ContractId');
    var MemberId = request('MemberId');
    var Type = request('Type');
    var ReLeaseType = 0;
    var AuditLevel = 0;
    var IsBack = request('IsBack');
    var NowLevel = -1;
    var ShowType = 0;
    var AuditConditionData = [];
    var Version = "";
    $(function () {
        InitControl();
    })
    //初始化数据
    function InitControl() {
        //组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
            }
        });
        if (!!ContractId) {
            $.SetForm({
                url: "../../QualificationManage/Contract/GetInfo",
                param: { keyValue: keyValue, ContractId: ContractId },
                success: function (data) {
                    //ContractId = data.ContractDWelling.Id;
                    ShowType = data.CBBInfo.IsShowHtml;
                    AuditLevel = data.CBBInfo.CurrentAuditLevel;  
                    var State = "待提交";
                    if (Type == 2) {
                        $("#Info1").SetWebControls(data.CBBInfo);
                        $("#Info2").hide();
                        $("#ReLeaseDate").removeAttr("checkexpession");
                    }
                    else if (Type == 3 || Type == 4) {
                        $("#Info2").SetWebControls(data.CBBInfo);
                        $("#Info1").hide();
                        $("#RentYearDate1").removeAttr("checkexpession");
                        $("#EndRenewDate").removeAttr("checkexpession");
                    }
                    if (!!data.CBBInfo) {
                        $("#CBB").SetWebControls(data.CBBInfo);
                        $("#Audit").SetWebControls(data.CBBInfo);
                        MemberId = data.CBBInfo.CreateUserId;
                        if (Type == 2) $("#RentYearDate1").val(GetTimes(data.ContractDWelling.EndDate, data.CBBInfo.EndRenewDate));
                        if (data.CBBInfo.State == 1) {
                            State = "审批中";
                            $("#btn_Submit").attr("disabled", "disabled").hide();
                            $(".BaseClass").attr("disabled", "disabled");
                            if (data.CBBInfo.ReLeaseType == 0) {
                                $("#DQ_Rdo").attr("checked", "checked");
                            }
                            else if (data.CBBInfo.ReLeaseType == 1) {
                                $("#TQ_Rdo").attr("checked", "checked");
                            }
                            $(".Rdo").attr("disabled", "disabled");
                            $("#Audit").show();
                            GetGrid(1);
                            AuditLevel = data.CBBInfo.CurrentAuditLevel;

                        }
                        else if (data.CBBInfo.State == 2) {
                            State = "审批通过";
                            $("#btn_Submit").attr("disabled", "disabled").hide();
                            $(".BaseClass").attr("disabled", "disabled");
                            if (data.CBBInfo.ReLeaseType == 0) {
                                $("#DQ_Rdo").attr("checked", "checked");
                            }
                            else if (data.CBBInfo.ReLeaseType == 1) {
                                $("#TQ_Rdo").attr("checked", "checked");
                            }
                            $(".Rdo").attr("disabled", "disabled");
                            $("#Audit").show();
                            GetGrid();
                            $("#AuditState").hide();

                        }
                        else if (data.CBBInfo.State == 3) {
                            State = "审批未通过";
                            $("#btn_Submit").attr("disabled", "disabled").hide();
                            $(".BaseClass").attr("disabled", "disabled");
                            if (data.CBBInfo.ReLeaseType == 0) {
                                $("#DQ_Rdo").attr("checked", "checked");
                            }
                            else if (data.CBBInfo.ReLeaseType == 1) {
                                $("#TQ_Rdo").attr("checked", "checked");
                            }
                            $("#Audit").show();
                            GetGrid();
                            $("#AuditState").hide();
                        }
                    }
                    $("#State").text(State);
                    $("#Info").SetWebControls(data.ContractDWelling);
                    $(".MemberInfo").SetWebControls(data.MemberInfo);
                    $("#PStruct").SetWebControls(data.PStruct);
                    $("#OrganizeId").ComboBoxTreeSetValue(data.PStruct.OrganizeId);
                    $("#PStruct").SetWebControls(data.PStruct);

                    if (Type == 3 || Type == 4) {
                        AuditLevel = data.CBBInfo.CurrentAuditLevel;
                        $("#Refundable").removeAttr("hidden"); 
                        $("#RefundableRent").text(data.CBBInfo.RefundableRent);
                        $("#RefundableDeposit").text(data.CBBInfo.RefundableDeposit);
                    }
                    $("#Area").val(data.Room.Area);
                    $.SetForm({
                        url: "../../SystemManage/DataItemDetail/GetDataItemListJson",
                        param: { EnCode: "HouseType" },
                        success: function (itemData) {
                            $(itemData).each(function (index, item) {
                                if (item.ItemValue == data.Room.HouseTypeName) {
                                    $("#HouseTypeName").val(item.ItemName);
                                    return;
                                }
                            });
                        }
                    });
                    $("#RoomCode").val(data.PStruct.Code);
                    $("#MonthlyRent").val(data.PStructInfo.MonthlyRent);
                    $("#RCode").val(data.ContractDWelling.Code);
                    $("#Date").val(formatDate($("#Date").val(), 'yyyy-MM-dd'));
                    $("#SignDate").val(formatDate($("#SignDate").val(), 'yyyy-MM-dd'));
                    $("#BeginDate").val(formatDate($("#BeginDate").val(), 'yyyy-MM-dd'));
                    $("#EndDate").val(formatDate($("#EndDate").val(), 'yyyy-MM-dd'));
                    $("#EndRenewDate").val(formatDate($("#EndRenewDate").val(), 'yyyy-MM-dd'));
                    $("#ReLeaseDate").val(formatDate($("#ReLeaseDate").val(), 'yyyy-MM-dd'));
                    $("#ModifyDate").val(formatDate($("#ModifyDate").val(), 'yyyy-MM-dd'));
                    if (data.ContractDWelling.BeginDate != "" && data.ContractDWelling.EndDate != "") {
                        $("#RentYearDate").val(GetTimes(data.ContractDWelling.BeginDate, data.ContractDWelling.EndDate));
                    }
                    if (data.CBBInfo.CurrentAuditLevel != -1 && (Type == 3 || Type == 4)) {
                        AuditLevel = data.CBBInfo.CurrentAuditLevel;
                        $("#AuditState").show();
                        GetIsShow("退租单");
                    }

                }
            })
        }
    }
    //获取数据
    function GetTimes(sdate, edate) {

        var Sdate = new Date(Date.parse(sdate.replace(/-/g, "/"))).getTime();
        var Edate = new Date(Date.parse(edate.replace(/-/g, "/"))).getTime();
        var a = Math.floor((Edate - Sdate) / 86400000);
        var y = Math.floor(a / 365);
        var m = Math.floor((a - y * 365) / 30);
        var d = (a - y * 365 - m * 30);
        if (a > 740) {
            dialogMsg("租赁时限不能大于两年。", 0);
            return false;
        }
        if (d > 20) {
            m++;
        }
        if (m >= 12) {
            y++;
            m = m - 12;
        }
        var RentYearDate = "";
        RentYearDate = y + "年" + m + "个月";
        if (m == 0) {
            RentYearDate = y + "年";
        }
        return RentYearDate;
    }
    //计算时间戳
    function GetDateTimes() {

        var BeginDate = $("#EndDate").val();
        var EndDate = $("#EndRenewDate").val();
        if (EndDate < BeginDate) {
            //dialogMsg("租赁结束时间必须大于之前合同的结束时间。", 0);
            return false;
        }
        if (BeginDate != "" && EndDate != "") {
            $("#RentYearDate1").val(GetTimes(BeginDate, EndDate));
        }
    }
    function btn_Save(Type) {

        var queryJson = {
            ContractId: ContractId,
            CreateUserId: MemberId,
            Code: $("#Code").val(),
            RenewYear: $("#RenewYear").val(),
            EndRenewDate: $("#EndRenewDate").val(),
            Description: $("#Description").val(),
            ReLeaseDate: $("#ReLeaseDate").val(),
            ReLeaseType: ReLeaseType,
            Type: Type
        };
        if (!$('#Info1').Validform()) {
            return false;
        }
        dialogConfirm('提交后将无法更改，是否提交？', function (r) {
            if (r) {
                queryJson["State"] = 1;
                $.SaveForm({
                    url: "../../QualificationManage/Contract/SaveForm?keyValue=" + keyValue + "&queryJson=" + JSON.stringify(queryJson),
                    loading: "正在保存数据...",
                    success: function () {
                        dialogClose();
                        $.currentIframe().$("#gridTable").resetSelection();
                        $.currentIframe().$("#gridTable").trigger("reloadGrid");
                    }
                })
            }
        });
    }

    function ChkReLeaseType(s) {
        if (s == 0) {
            $("#ReLeaseDate").val($("#EndDate").val());
            $("#ReLeaseDate").attr("disabled", "disabled");
            Type = 3;
        }
        else {
            Type = 4
            $("#ReLeaseDate").removeAttr("disabled");
        }
        ReLeaseType = s;
    }
    //加载审核记录
    function GetGrid(State) {
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            url: "../../QualificationManage/Contract/GetAuditList?BussinessId=" + keyValue,
            datatype: "json",
            height: 110,
            autowidth: true,
            unwritten: false,
            colModel: [
                    { label: 'ID', name: 'Id', hidden: true },
                    {
                        label: "审批结果", name: "AuditState", width: 100, align: "center", sortable: true,
                        formatter: function (cellvalue, options, rowObject) {
                            switch (cellvalue) {
                                case 2: return "不通过";
                                case 1: return "通过";
                                default: return "";
                            }
                        }
                    },
                    { label: '审批人', name: 'AuditMan', width: 100, align: "center", sortable: false },
                    { label: "审批时间", name: "AuditTime", width: 200, align: "center", sortable: false },
                    { label: "审批意见", name: "AuditMemo", width: 400, align: "left", sortable: false },
                    {
                        label: "审批操作", name: "AuditCondition", width: 100, align: "left", sortable: false,
                        formatter: function (cellvalue, options, rowObject) {
                            if (!!cellvalue) {
                                var data = JSON.parse(cellvalue);
                                for (var i = 0; i < data.length; i++) {
                                    AuditConditionData.push(data[i]);
                                    if (data[i].Fieldvalue == 1 || data[i].Fieldvalue == "1") {
                                        return data[i].FieldName;
                                    }
                                    return "";
                                }
                            }
                            return "";
                        }
                    }

            ],
            rowNum: "1000",
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false,
            gridComplete: function () {
                if (State == 1) {
                    var typeName = Type == 2 ? "续租单" : "退租单";
                    if (AuditLevel != -1) {
                         GetIsShow(typeName);
                    }
                }
            }
        });
    }

    function btn_Audit(AuditState) {
        if (!$('#form1').Validform()) {
            return false;
        }
        var queryJson = $("#Audit").GetWebControls(keyValue);
        queryJson["AuditState"] = AuditState;
        queryJson["Level"] = AuditLevel + 1;
        queryJson["BussinessId"] = keyValue;
        queryJson["ContractId"] = ContractId;
        queryJson["AuditType"] = 0;
        queryJson["AuditMemo"] = $("#AuditMemo").val();
        queryJson["Version"] = Version;
        var AuditCondition = [];
        debugger;
        $(".WorkAudit").each(function () {
            var FieldId = $(this).attr("Id");
            var FieldName = $(this).attr("Title");
            if ($(this).attr("type") == "checkbox") AuditCondition.push({ FieldId: FieldId, FieldName: FieldName, Fieldvalue: $(this).is(":checked") ? 1 : 0, FieldDate: new Date() })
            else AuditCondition.push({ FieldId: FieldId, FieldName: FieldName, Fieldvalue: $(this).val(), FieldDate: new Date() })
        }) 
        queryJson["AuditCondition"] = JSON.stringify(AuditCondition);
        queryJson["AuditConditionData"] = JSON.stringify(AuditConditionData);
        queryJson["Level"] = NowLevel;
       
        if (AuditState == 2 && $("#AuditMemo").val() == "") {
            dialogMsg("不同意请说明原因！", 0);
            return false;
        } 
        $.ConfirmAjax({
            msg: "注：您确认要进行此操作吗？",
            url: "../../QualificationManage/Contract/SaveAuditForm",
            param: { queryJson: JSON.stringify(queryJson) },
            loading: "正在保存数据...",
            success: function () {
                dialogClose();
                $.currentIframe().$("#gridTable").resetSelection();
                $.currentIframe().$("#gridTable").trigger("reloadGrid");

            }
        });

    }

    function GetIsShow(TypeName) {
        if (AuditLevel == 0) AuditLevel = 1;
        $.SetForm({
            url: "../../SystemManage/Setting/GetIsShow",
            param: { TypeName: TypeName, Level: AuditLevel,Version:Version },
            dataType: "text",
            success: function (data) {
                var Audit = JSON.parse(data);
                var Html = "";
                if (Audit.OK) {
                    $("#AuditState").show();
                    NowLevel = Audit.NowLevel;
                    if (Audit.HasHandling == 1) {
                        var HandleConditionJson = JSON.parse(Audit.HandleConditionJson);
                        for (var i = 0; i < HandleConditionJson.length; i++) {
                            if (HandleConditionJson[i].Type == "1") {
                                Html += "<td class=\"formTitle\"><input id=\"" + HandleConditionJson[i].FieldId + "\" type=\"checkbox\" class=\"WorkAudit\" title=\"" + HandleConditionJson[i].FieldName + "\" /></td>";
                                Html += "<td class=\"formValue\" >" + HandleConditionJson[i].FieldName + "</td>";
                            }
                            if (HandleConditionJson[i].Type == "2") {
                                Html += "<td class=\"formTitle\">" + HandleConditionJson[i].FieldName + "<font face=\"宋体\">*</font></td>";
                                Html += "<td class=\"formValue\"><input id=\"" + HandleConditionJson[i].FieldId + "\" type=\"text\" class=\"form-control WorkAudit\" isvalid=\"yes\" checkexpession=\"NotNull\" title=\"" + HandleConditionJson[i].FieldName + "\" /></td>";
                            }
                        }
                        if (ShowType != 0) {
                            $("#WorkAudit").html(Html);
                        }


                    }
                }
                else
                    $("#AuditState").hide();
            }
        });
    }

    function AcceptClick() {
        if (!!IsBack) {
            btn_Save(Type);
        } else {
            dialogClose();
        }

    }
</script>
<style>
    #Info, #Info td {
        border: 1px solid #ccc;
    }

    .cs-1 {
        background-color: #DDF0ED;
    }

    .cs-2 {
        border: none;
    }
</style>
<div>
    <table class="form" id="CBB" style="margin-top:5px; width:90%; margin-left:5%;">
        <tr>
            <td class="formTitle">单据状态:</td>
            <td class="formValue"><span id="State" style="color:red;font-size:20px;"></span></td>
            <td class="formTitle">申请单编号:</td>
            <td class="formValue"><input class="form-control" disabled placeholder="系统自动编号" id="Code" readonly type="text" /></td>
            <td class="formTitle">申请日期:</td>
            <td class="formValue"><input id="Date" type="text" disabled class="form-control input-wdatepicker BaseClass " readonly placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
        </tr>
    </table>

    <table class="form" id="Info" style="margin-top: 20px; width: 90%; margin-left: 5%;">
        <tr>
            <td class="formTitle cs-1">所属机构</td>
            <td class="formValue"><div id="OrganizeId" type="selectTree" readonly class="ui-select cs-2 "></div></td>
            <td class="formTitle cs-1">合同编号</td>
            <td class="formValue"><input class="form-control cs-2" id="RCode" readonly type="text" /></td>
            <td class="formTitle cs-1">签约日期</td>
            <td class="formValue"><input id="SignDate" type="text" disabled class="form-control input-wdatepicker cs-2 " disabled placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
        </tr>
        <tr id="PStruct">
            <td class="formTitle cs-1">租赁房屋</td>
            <td class="formValue" colspan="3"><input class="form-control cs-2" id="AllName" readonly type="text" /></td>
            <td class="formTitle cs-1">房屋编号</td>
            <td class="formValue"><input class="form-control cs-2" id="RoomCode" readonly type="text" /></td>
        </tr>
        <tr id="Room">
            <td class="formTitle cs-1">建筑面积</td>
            <td class="formValue"><input class="form-control cs-2" id="Area" readonly type="text" /></td>
            <td class="formTitle cs-1">户型</td>
            <td class="formValue"><input class="form-control cs-2" id="HouseTypeName" readonly type="text" /></td>
            <td class="formTitle cs-1">每月租金</td>
            <td class="formValue"><input class="form-control cs-2" id="MonthlyRent" readonly type="text" /></td>
        </tr>
        <tr class="MemberInfo">
            <td class="formTitle cs-1">承租人</td>
            <td class="formValue"><input class="form-control cs-2" id="Name" readonly type="text" /></td>
            <td class="formTitle cs-1">身份证号</td>
            <td class="formValue"><input class="form-control cs-2" id="IDCard" readonly type="text" /></td>
            <td class="formTitle cs-1">联系电话</td>
            <td class="formValue"><input class="form-control cs-2" id="Phone" readonly type="text" /></td>
        </tr>
        <tr class="MemberInfo">
            <td class="formTitle cs-1">所属单位</td>
            <td class="formValue"><input class="form-control cs-2" id="CompanyName" readonly type="text" /></td>
            <td class="formTitle cs-1">单位地址</td>
            <td class="formValue"><input class="form-control cs-2" id="CompanyAddress" readonly type="text" /></td>
            <td class="formTitle cs-1">单位电话</td>
            <td class="formValue"><input class="form-control cs-2" id="CompanyPhone" readonly type="text" /></td>
        </tr>
        <tr>
            <td class="formTitle cs-1">租赁开始时间</td>
            <td class="formValue"><input id="BeginDate" type="text" disabled readonly class="form-control input-wdatepicker cs-2 " readonly placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd ")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
            <td class="formTitle cs-1">租赁结束时间</td>
            <td class="formValue"><input id="EndDate" type="text" disabled readonly class="form-control input-wdatepicker cs-2 " readonly placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd ")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
            <td class="formTitle cs-1">租赁年限</td>

            <td class="formValue">
                <input id="RentYear" class="form-control" type="hidden" />
                <input class="form-control cs-2" id="RentYearDate" readonly type="text" />
            </td>
        </tr>

    </table>
    <table class="form" id="Info1" style="margin-top: 20px; width: 90%; margin-left: 5%;">
        <tr>
            <td class="formTitle">续租年限</td>
            <td class="formValue">
                <input id="RenewYear" class="form-control" type="hidden" />
                <input id="RentYearDate1" readonly disabled class="form-control BaseClass" placeholder="选择终止日期后自动生成" type="text" isvalid="yes" checkexpession="NotNull" />
            </td>
            <td class="formTitle">租赁终止日期</td>
            <td class="formValue"><input id="EndRenewDate" type="text" isvalid="yes" checkexpession="NotNull" class="form-control input-wdatepicker BaseClass " onblur="GetDateTimes();" placeholder="请输入申请日期" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ', minDate: '#F{$dp.$D(\'EndDate\')}' }) " /></td>
        </tr>
        <tr>
            <td class="formTitle">附加说明</td>
            <td class="formValue" colspan="5"><textarea id="Description" class="form-control BaseClass" style="height: 80px;" isvalid="yes" checkexpession="IsScript"></textarea></td>
        </tr>
        <tr style="text-align:center;"></tr>
    </table>
    <table class="form" id="Info2" style="margin-top: 10px; width: 90%; margin-left: 5%;">
        <tr>
            <td class="formTitle">退租类型：</td>
            <td class="formTitle"><input id="DQ_Rdo" class="Rdo" name="Rdo_tu" type="radio" onchange="ChkReLeaseType(0);" />到期退租</td>
            <td class="formTitle"><input id="TQ_Rdo" class="Rdo" name="Rdo_tu" type="radio" onchange="ChkReLeaseType(1);" />提前退租</td>
            <td class="formTitle">申请终止日期;</td>
            <td class="formValue"><input id="ReLeaseDate" type="text" isvalid="yes" checkexpession="IsScript" class="form-control input-wdatepicker BaseClass " placeholder="请输入申请日期" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ', maxDate: '#F{$dp.$D(\'EndDate\')}' }) " /></td>
        </tr>
        <tr>
            <td class="formTitle">附加说明</td>
            <td class="formValue" colspan="5"><textarea id="Description" isvalid="yes" checkexpession="IsScript" class="form-control BaseClass" style="height: 80px;"></textarea></td>
        </tr>
        <tr style="text-align:center;"></tr>
    </table>
    <table class="form" id="Audit" style="margin-top: 10px; width: 90%; margin-left: 5%;display:none;">
        <tr>
            <td class="formTitle">申请人：</td>
            <td class="formValue"><input id="CreateUserName" readonly disabled class="form-control" isvalid="yes" checkexpession="IsScript" type="text" /></td>
            <td class="formTitle">申请提交时间：</td>
            <td class="formValue"><input id="ModifyDate" readonly disabled class="form-control" isvalid="yes" checkexpession="IsScript" type="text" /></td>
        </tr>
        <tr id="Refundable" hidden>
            <td class="formTitle">可退租金：</td>
            <td class="formValue"><input id="RefundableRent" readonly disabled class="form-control" isvalid="yes" checkexpession="IsScript" type="text" /></td>
            <td class="formTitle">可退押金：</td>
            <td class="formValue"><input id="RefundableDeposit" readonly disabled class="form-control" isvalid="yes" checkexpession="IsScript" type="text" /></td>
        </tr>
        <tr>
            <td colspan="4">
                <table id="gridTable"></table>
            </td>
        </tr>
        <tr id="WorkAudit"></tr>
        <tr id="AuditState">
            <td class="formTitle">审批意见：</td>
            <td class="formValue" colspan="2"><input id="AuditMemo" class="form-control" type="text" isvalid="yes" checkexpession="IsScript" /></td>
            <td class="formValue">
                <a id="lr-addRule" class="btn btn-success btn-xs" style="width:150px;height:28px;" onclick="btn_Audit(1)">同意</a>
                <a id="lr-deleteRule" class="btn btn-danger btn-xs" style="width: 150px; height: 28px;" onclick="btn_Audit(2)">不同意</a>
            </td>
        </tr>
    </table>
</div>

